/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;
using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	public class GS_CheckPlayers : GameState
	{
		Surface _sfFrame;
		
		// Elements location
		Rectangle _rctFrame;
		
		// Labels
		Label _lblTitle;
		Label[] _lblPlayers;
		Label[] _lblInstructions;
		Label _lblMessage;
		
		int[] _iPlayersState;
		
		public GS_CheckPlayers()
		{
			// Initializing the Labels
			// Title
			_lblTitle = new Label(LanguageManager.Get("CP_Title"), 24, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			_lblTitle.Location = new Point(0,15);
			// Players & Instructions
			_lblPlayers = new Label[PlayerManager.Count];
			_lblInstructions = new Label[PlayerManager.Count];
			for(int i=0; i<PlayerManager.Count; i++)
			{
				_lblPlayers[i] = new Label(PlayerManager.GetName(i), 30, Color.Black, TextAlignment.TopLeft);
				_lblInstructions[i] = new Label("", 26, Color.Black, TextAlignment.TopLeft);
			}
			// Message
			_lblMessage = new Label(LanguageManager.Get("CP_Press"), 24, Color.AntiqueWhite, TextAlignment.TopMiddle);
			_lblMessage.AutoHeight = true;
			_lblMessage.Location = new Point(0, 450);
			
			// Initializing the Players' state
			_iPlayersState = new int[PlayerManager.Count];
			for(int i=0; i<PlayerManager.Count; i++)
			{
				switch(PlayerManager.GetState(i))
				{
					case PlayerState.inGame:
						_iPlayersState[i] = 1;
						break;
					case PlayerState.outGame:
						_iPlayersState[i] = -1;
						break;
				}
			}
		}
		
		public void Draw()
		{	
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			for(int i=0; i<PlayerManager.Count; i++)
			{
				ScreenManager.MainScreen.Blit(_lblPlayers[i].Surface, _lblPlayers[i].Location);
				ScreenManager.MainScreen.Blit(_lblInstructions[i].Surface, _lblInstructions[i].Location);
			}			
		}
		
		public void CleanUp()
		{
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				KeyboardState state = new KeyboardState();
				// Each player active has to press his key
				for (int i=0; i<PlayerManager.Count; i++)
				{
					if ((_iPlayersState[i]==1) && state.IsKeyPressed(PlayerManager.GetKey(i)))
					{
						// Updating the player's state
						_iPlayersState[i]=0;
						_lblPlayers[i].TextColor = Color.Green;
						_lblInstructions[i].TextColor = Color.Green;
						_lblInstructions[i].Text = LanguageManager.Get("CP_Checked");
						_lblPlayers[i].Init();
						_lblInstructions[i].Init();
					}
				}
			}
		}
		
		public void Init()
		{
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150, 50), _sfFrame.Size);
			
			// Initializing the title
			_lblTitle.Size = _rctFrame.Size;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the message
			_lblMessage.Width = _rctFrame.Width;
			_lblMessage.Init();
			_sfFrame.Blit(_lblMessage.Surface, _lblMessage.Location);
			
			// Initializing the players' names and instructions
			for(int i=0; i<PlayerManager.Count; i++)
			{
				Color playerColor = Color.Black;
				switch(_iPlayersState[i])
				{
					case -1:
						playerColor = Color.Red;
						_lblInstructions[i].Text = LanguageManager.Get("CP_OutOfGame");
						break;
					case 0:
						playerColor = Color.Green;
						_lblInstructions[i].Text = LanguageManager.Get("CP_Checked");
						break;
					case 1:
						playerColor = Color.Orange;
						_lblInstructions[i].Text = LanguageManager.Get("CP_Unchecked");
						break;
				}
				
				// Names
				_lblPlayers[i].Location = new Point(_rctFrame.X + 20, _rctFrame.Y + 70 + i*100);
				_lblPlayers[i].TextColor = playerColor;
				_lblPlayers[i].AutoHeight = true;
				_lblPlayers[i].AutoWidth = true;
				_lblPlayers[i].Init();
				// Instructions
				_lblInstructions[i].Location = new Point(_rctFrame.X + 50, _rctFrame.Y + 110 + i*100);
				_lblInstructions[i].TextColor = playerColor;
				_lblInstructions[i].AutoHeight = true;
				_lblInstructions[i].AutoWidth = true;
				_lblInstructions[i].Init();
			}
		}
		
		public void Update()
		{
			bool bResult = true;
			for(int i=0; i<PlayerManager.Count; i++)
				bResult &= (_iPlayersState[i]<=0);
			
			if (bResult)
				GameStateManager.ChangeState(new GS_CountDown());
		}
	}
}
